// pages/profile/person-info/index.js
import UserApi from '../../../api/user'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    //用户输入的学号
    number: '',
    //用户输入的昵称
    nickName: '',
    //用户输入的真实姓名
    realName: '',
    //用户输入的手机号
    phone: '',
    // 用户输入的邮箱
    email: wx.getStorageSync('user').email,
    // 控制修改学号弹出框
    showEditNumberDialog: false,
    // 控制修改昵称弹出框
    showEditNickNameDialog: false,
    // 控制修改真实姓名弹出框
    showEditRealNameDialog: false,
    // 控制修改专业弹出框
    showEditProfessionPicker: false,
    // 控制修改年级弹出框
    // showEditGradeDialog: false,
    showEditGradePicker: false,
    //控制修改手机号弹出框
    showEditPhoneDialog: false,
    // 控制修改邮箱弹出框
    showEmailDialog: false,
    columns: ['计算机科学与技术', '计算机科学与技术(卓越)', '软件工程', '智能科学', '信息对抗', '信息安全'],
    currentYear: new Date().getFullYear(),
    grades: [],
    exit: false,
    time: 0,
    code: '',
    newPassword: '',
    confirmPassword:'',
    showPasswordDialog: false,
  },
  showPasswordDialog() {
    this.setData({
      showPasswordDialog: true
    })
  },
  inputNewPassword({detail}) {
    this.setData({
      newPassword: detail
    })
  },
  inputConfirmPassword({detail}) {
    this.setData({
      confirmPassword: detail
    })
  },
  onClosePasswordDialog() {
    this.setData({
      newPassword: '',
      confirmPassword: '',
      time: 0,
    })
  },
  editPassword() {
    if (this.data.newPassword !== this.data.confirmPassword) {
      this.setData({
        newPassword: '',
        confirmPassword: '',
      })
      return wx.showToast({
        title: '两次密码不一致',
        icon: 'none'
      })
    }
    wx.showLoading({
      title: '修改中...'
    })
    let params = {
      verificationCode: this.data.code,
      newPassword: this.data.newPassword,
      confirmPassword: this.data.confirmPassword
    }
    UserApi.editPassword(params).then(res => {
      wx.hideLoading()
      if (res.data.code !== 200) {
        wx.showToast({
          title: res.data.data,
          icon: 'none'
        })
      }else {
        wx.showToast({
          title: '修改成功，请重新登录！',
          icon: 'none'
        })
      }
    })
    this.setData({
      newPassword: '',
      confirmPassword: '',
      code: '',
      time: 0,
    })
    wx.hideLoading()

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      userInfo: wx.getStorageSync('user'),
      grades: [this.data.currentYear.toString(), (this.data.currentYear - 1).toString(),
        (this.data.currentYear - 2).toString(),
        (this.data.currentYear - 3).toString(),
        (this.data.currentYear - 4).toString()
      ]
    })
  },
  // 当邮箱输入框内容改变时触发
  inputEmail(event) {
    this.setData({
      email: event.detail
    })
  },
  // 获取验证码
  getCode() {
    wx.showLoading({
      title: '发送中...'
    })
    this.setData({
      time: 60
    })
    var timer = setInterval(() => {
      if (this.data.time == 0) {
        clearInterval(timer);
      } else {
        this.setData({
          time: this.data.time - 1
        })
      }
    }, 1000);
    console.log(this.data.email);
    UserApi.getCode(this.data.email).then(res => {
      if (res.data.code !== 200) {
        wx.hideLoading()
        return wx.showToast({
          title: res.data.data,
          icon: "none"
        })
      }
      wx.hideLoading()
      wx.showToast({
        title: '发送成功！',
        icon: 'success'
      })
    })
  },
  // 确认绑定邮箱时触发
  editEmail() {
    // let oldEmail = this.data.userInfo.email
    // this.setData({
    //   ['userInfo.email']: this.data.email
    // })
    let that = this
    wx.showLoading({
      title: '修改中...',
    })
    let params = {
      email: this.data.email,
      code: this.data.code
    }
    UserApi.bindEmail(params).then(res => {
      wx.hideLoading()
      if (res.data.code !== 200) {
        wx.showToast({
          title: '绑定失败: ' + res.data.data,
          icon: "none"
        })
      } else {
        wx.setStorageSync("user", res.data.data)
        that.setData({
          userInfo: res.data.data
        })
        wx.showToast({
          title: '绑定成功！',
          icon: "success"
        })
      }
    })
    this.setData({
      email: wx.getStorageSync("user").email,
      code: '',
      time: 0,
    })
  },
  // 点击关闭邮箱弹框时触发
  onCloseEditEmailDialog() {
    this.setData({
      code: '',
      time: 0,
      showEmailDialog: false,
      email: wx.getStorageSync('user').email,
      ['userInfo.email']: wx.getStorageSync('user').email
    })
  },
  //点击“学号/工号”弹出对话框
  shownumberDialog() {
    this.setData({
      showEditNumberDialog: true
    })
  },
  showEmailDialog() {
    this.setData({
      showEmailDialog: true
    })
  },
  onCloseEditNumberDialog() {
    this.setData({
      showEditNumberDialog: false,
      number: wx.getStorageSync('user').number,
      ['userInfo.number']: wx.getStorageSync('user').number
    })
  },
  //当学号输入框失去焦点
  verify(e) {
    this.setData({
      number: e.detail.value
    })
  },
  //确定修改学号
  editNumber() {
    wx.showLoading({
      title: "加载中..."
    })
    var oldNumber = this.data.userInfo.number
    this.setData({
      ['userInfo.number']: this.data.number
    })
    let that = this
    UserApi.updateOwn(this.data.userInfo).then(data => {
      if (data.data.code !== 200) {
        that.setData({
          ['userInfo.number']: oldNumber
        })
        return wx.showToast({
          title: data.data.data,
          icon: "none"
        })
      }
      wx.setStorageSync('user', that.data.userInfo)
      return wx.showToast({
        title: '修改成功！',
        icon: 'success'
      })
    })
    wx.hideLoading()
  },
  //点击“昵称”弹出对话框
  showNickNameDialog() {
    this.setData({
      showEditNickNameDialog: true
    })
  },
  onCloseEditNickNameDialog() {
    this.setData({
      showEditNickNameDialog: false,
      nickName: wx.getStorageSync('user').nickname,
      ['userInfo.nickname']: wx.getStorageSync('user').nickname
    })
  },
  //当昵称输入框失去焦点
  verifyNickName(e) {
    this.setData({
      nickName: e.detail.value
    })
  },
  //确定修改昵称
  editNickName() {
    wx.showLoading({
      title: "加载中..."
    })
    var oldNickName = this.data.userInfo.nickname
    this.setData({
      ['userInfo.nickname']: this.data.nickName
    })
    let that = this
    UserApi.updateOwn(this.data.userInfo).then(data => {
    wx.hideLoading()
      if (data.data.code !== 200) {
        that.setData({
          ['userInfo.nickname']: oldNickName
        })
        return wx.showToast({
          title: data.data.data,
          icon: 'none'
        })
      }
      wx.setStorageSync('user', that.data.userInfo)
      return wx.showToast({
        title: '修改成功！',
        icon: 'success'
      })
    })
  },
  //点击“真实姓名”弹出对话框
  showRealNameDialog() {
    this.setData({
      showEditRealNameDialog: true
    })
  },
  //当点击弹框“取消”时
  onCloseEditRealNameDialog() {
    this.setData({
      showEditRealNameDialog: false,
      realName: wx.getStorageSync('user').realName,
      ['userInfo.realName']: wx.getStorageSync('user').realName
    })
  },
  //当真实姓名输入框失去焦点
  verifyRealName(e) {
    this.setData({
      realName: e.detail.value
    })
  },
  //确定修改真实姓名
  editRealName() {
    wx.showLoading({
      title: "加载中..."
    })
    var oldRealName = this.data.userInfo.realName
    this.setData({
      ['userInfo.realName']: this.data.realName
    })
    let that = this
    UserApi.updateOwn(this.data.userInfo).then(data => {
      wx.hideLoading()
      if (data.data.code !== 200) {
        that.setData({
          ['userInfo.realName']: oldRealName
        })
        return wx.showToast({
          title: data.data.data,
          icon: 'none'
        })
      }
      wx.setStorageSync('user', that.data.userInfo)
      return wx.showToast({
        title: '修改成功！',
        icon: 'success'
      })
    })
  },
  //点击“专业”弹出
  showProfessionPicker() {
    this.setData({
      showEditProfessionPicker: true
    })
  },
  onProfessionPickerClose() {
    this.setData({
      showEditProfessionPicker: false
    })
  },
  //点击确定修改专业
  onProfessionConfirm(e) {
    wx.showLoading({
      title: "加载中..."
    })
    var oldProfession = this.data.userInfo.profession
    this.setData({
      ['userInfo.profession']: e.detail.value
    })
    let that = this
    UserApi.updateOwn(this.data.userInfo).then(data => {
      if (data.data.code !== 200) {
        that.setData({
          ['userInfo.profession']: oldProfession,
          showEditProfessionPicker: false
        })
        return wx.showToast({
          title: data.data.data,
          icon: "none"
        })
      }
      wx.setStorageSync('user', that.data.userInfo)
      this.setData({
        showEditProfessionPicker: false
      })
      return wx.showToast({
        title: '修改成功！',
        icon: 'success'
      })
    })
    wx.hideLoading()
  },

  //点击“年级”弹出
  showGradePicker() {
    this.setData({
      showEditGradePicker: true
    })
  },
  onGradePickerClose() {
    this.setData({
      showEditGradePicker: false
    })
  },
  //点击确定修改年级
  onGradeConfirm(e) {
    wx.showLoading({
      title: "加载中..."
    })
    var oldGrade = this.data.userInfo.grade
    this.setData({
      ['userInfo.grade']: e.detail.value
    })
    let that = this
    UserApi.updateOwn(this.data.userInfo).then(data => {
      if (data.data.code !== 200) {
        that.setData({
          ['userInfo.grade']: oldGrade,
          showEditGradePicker: false
        })
        return wx.showToast({
          title: data.data.data,
          icon: 'none'
        })
      }
      wx.setStorageSync('user', that.data.userInfo)
      this.setData({
        showEditGradePicker: false
      })
      return wx.showToast({
        title: '修改成功！',
        icon: 'success'
      })
    })
    wx.hideLoading()
  },
  showPhoneDialog() {
    this.setData({
      showEditPhoneDialog: true
    })
  },
  onCloseEditPhoneDialog() {
    this.setData({
      showEditPhoneDialog: false,
      phone: wx.getStorageSync('user').phone,
      ['userInfo.phone']: wx.getStorageSync('user').phone
    })
  },
  //当手机号输入框失去焦点
  verifyPhone(e) {
    this.setData({
      phone: e.detail.value
    })
  },
  //确定修改手机号
  editPhone() {
    wx.showLoading({
      title: "加载中..."
    })
    //判断手机号是否合法
    if (this.data.phone.length == 11) {
      const regex = /^(((1[35789][0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/
      if (regex.test(this.data.phone)) {
        //如果手机号合法
        var oldPhone = this.data.userInfo.phone
        this.setData({
          ['userInfo.phone']: this.data.phone
        })
        let that = this
        UserApi.updateOwn(this.data.userInfo).then(data => {
          if (data.data.code !== 200) {
            that.setData({
              ['userInfo.phone']: oldPhone
            })
            return wx.showToast({
              title: data.data.data,
              icon: 'none'
            })
          }
          wx.setStorageSync('user', that.data.userInfo)
          return wx.showToast({
            title: '修改成功！',
            icon: 'success'
          })
        })
      } else {
        return wx.showToast({
          title: '手机号不合法,修改失败！',
          icon: 'none'
        })
      }
    } else {
      return wx.showToast({
        title: '手机号长度错误，修改失败！',
        icon: 'none'
      })
    }
    wx.hideLoading()
  },
  exitLogin() {
    this.setData({
      exit: true
    })
  },
  //关闭退出登录弹框
  onClose() {
    this.setData({
      exit: false
    });
  },
  //退出登录
  logout() {
    this.setData({
      userInfo: null
    })
    wx.removeStorageSync('user')
    wx.removeStorageSync('token')
    wx.switchTab({
      url: '../../index/index',
    })
  },
})
